///////////////////////////////////////////////////////////////////////////////
// Project: Hoyo Client Game
//
// hy_battle_net_logic.cpp
// Created: 2013-7-15
// Author: Like Cheng
///////////////////////////////////////////////////////////////////////////////

#include "hy_battle_logic.h"
#include "hy_battle_net_logic.h"
#include "hy_net/hy_network_default.h"
#include "hy_net/hy_network_message.h"
#include "hy_battle_system/hy_battle_data.h"
#include "hy_battle_system/hy_battle_unit.h"
#include "hy_battle_effect_data.h"
#include "hy_network_message.h"
#include "game_server.h"
USING_NS_HY_NETWORK;

hy_battle_net_logic* hy_battle_net_logic_instance = NULL;
hy_battle_net_logic::hy_battle_net_logic()
{

}

hy_battle_net_logic::~hy_battle_net_logic()
{

}
hy_battle_net_logic* hy_battle_net_logic::instance()
{
	if( hy_battle_net_logic_instance == NULL )
		hy_battle_net_logic_instance = new hy_battle_net_logic;
	return hy_battle_net_logic_instance;
}
void hy_battle_net_logic::purge_instance()
{
	DEL_POINTER( hy_battle_net_logic_instance );
}


void hy_battle_net_logic::proc_battle_state( void* mg )
{
	hy_network_message* msg = (hy_network_message*)mg;

	int player_id = msg->get_player_id();
	int battle_id = msg->read_int( "battle_id" );
	int player_battle_state = msg->read_int( "battle_state" );

	hy_battle_data* bd = hy_battle_logic::instance()->get_battle_data( battle_id );
	if( !bd )return;

	hy_battle_unit*	hbu = bd->get_battle_unit_by_id_and_type( player_id, battle_unit_type_player );
	if( !hbu )return;

	hbu->m_client_state = (battle_client_state_enum)player_battle_state;//battle_state_ready_ok;

	hy_battle_logic::instance()->battle_state_proc( battle_id );
}

bool hy_battle_net_logic::proc_create_battle( void* mg )
{
	hy_network_message* msg = (hy_network_message*)mg;

	hy_battle_data* bd = hy_battle_logic::instance()->create_battle( msg->get_player_id(), 1 );

	if( bd )
	{
		hy_battle_logic::instance()->create_battle_progress( bd->get_id() );

		instance()->send_battle_info_msg( bd->get_id() );

		instance()->send_battle_progress_msg( bd->get_id() );

		return true;
	}
	return false;
}
Json::Value hy_battle_net_logic::get_battle_info_json_data( int battle_id )
{
	Json::Value bud;
	hy_battle_data* bd = hy_battle_logic::instance()->get_battle_data( battle_id );
	if( !bd )return bud;

	Json::Value create_jv( Json::arrayValue );
	hy_battle_unit_map::iterator it;
	for( it = bd->m_battle_unit_map.begin(); it != bd->m_battle_unit_map.end(); it++ )
	{
		hy_battle_unit* bu = it->second;
		create_jv.append( bu->get_json_value() );
	}
	bud["c"] = create_jv;

	/*
	Json::Value spm( Json::arrayValue );
	hy_tag_skill_pro_map::iterator it2;
	for( it2 = bd->m_exec_round->m_tag_skill_pro_map.begin(); 
		it2 != bd->m_exec_round->m_tag_skill_pro_map.end(); it2++ )
	{
		hy_tag_skill_pro& sp = it2->second;

		Json::Value jv;
		jv["r"] = sp.m_round;
		jv["sp"] = sp.m_send_pos;
		jv["ss"] = sp.m_send_skill;
		jv["sse"] = sp.m_send_skill_effect;
		jv["ch"] = sp.m_cur_hp;
		jv["ct"] = sp.m_cur_times;
		jv["cv"] = sp.m_current_value;
		jv["sh"] = sp.m_surplus_hp;

		Json::Value cv( Json::arrayValue );
		hy_tag_dis_hp_map::iterator it3;
		for( it3 = sp.m_tag_dis_hp_map.begin(); it3 != sp.m_tag_dis_hp_map.end(); it3++ )
		{
			hy_tag_dis_hp& tdh = it3->second;
			Json::Value cv2;
			cv2["ch"] = tdh.m_cur_hp;
			cv2["cp"] = tdh.m_cur_pos;
			cv2["cv"] = tdh.m_current_value;
			cv2["d"] = tdh.m_dodge;
			cv2["sc"] = tdh.m_state_change;
			cv2["sh"] = tdh.m_surplus_hp;
			cv2["t"] = tdh.m_type;
			cv.append( cv2 );
		}
		jv["td"] = cv;
		spm.append( jv );
	}
	bud["p"] = spm;
	*/

	return bud;
}


void hy_battle_net_logic::send_battle_info_msg( int battle_id )
{
	//hy_network_message* msg = (hy_network_message*)mg;

	hy_battle_data* bd = hy_battle_logic::instance()->get_battle_data( battle_id );
	if( !bd )return;

	Json::Value get_jv = get_battle_info_json_data( battle_id );

	int_vec iv = hy_battle_logic::instance()->get_player_list( battle_id );
	for( uint i = 0; i < iv.size(); i++ )
	{
		int player_id = iv.at( i );

		hy_network_message msg;
		msg.set_protocol_id( SID_BATTLE_INFO );
		msg.set_player_id( player_id );
		//msg.write_value( "p", get_jv );
		msg.set_value( get_jv );
		msg.send();
	}
}

void hy_battle_net_logic::send_battle_progress_msg( int battle_id )
{
	hy_battle_data* bd = hy_battle_logic::instance()->get_battle_data( battle_id );
	if( !bd )return;
	
	hy_tag_skill_pro_map::iterator  it;
	int_vec iv = hy_battle_logic::instance()->get_player_list( battle_id );
	for( uint i = 0; i < iv.size(); i++ )
	{
		int player_id = iv.at( i );
		
		//int i = 0;
		for( it = bd->m_exec_round->m_tag_skill_pro_map.begin(); 
			it != bd->m_exec_round->m_tag_skill_pro_map.end(); it++ )
		{
			hy_tag_skill_pro& hts = it->second;
			hy_network_message msg;
			msg.set_protocol_id( SID_BATTLE_PROCESS );
			msg.set_player_id( player_id );			
			msg.write_int( "r", hts.m_cur_times );
			msg.write_value( "d", hts.get_tag_skill_pro_json() );
			msg.send();
		}
		hy_network_message emsg;
		emsg.set_protocol_id( SID_BATTLE_PROCESS );
		emsg.set_player_id( player_id );
		emsg.write_int( "r", -1 );
		emsg.send();
	}
	
}